/**
 * @copyright Generali Solutions d'assurances - Tous droits reserves &copy; 2007 - 2010
 */
package fr.generali.accueilclient.utils.httpurlconnection;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Holmes Kelly
 */
public final class HttpURLConnectionUtils {

    /**
     * Le logger
     */
    private static final Logger logger = LoggerFactory.getLogger(HttpURLConnectionUtils.class);

    public static final String METHOD_GET = "GET";

    public static final String METHOD_POST = "POST";

    private HttpURLConnectionUtils() {

    }

    public static HttpURLConnection getConnection(String urlQuery, String query, String verb, String charset) {

        HttpURLConnection httpURLConnection = null;

        try {
            URL url = new URL(urlQuery);

            httpURLConnection = HttpURLConnection.class.cast(url.openConnection());
            httpURLConnection.setRequestMethod(verb);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestProperty("Accept-Charset", charset);
            httpURLConnection
                            .setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.write(query.getBytes(charset));
            dataOutputStream.flush();
            dataOutputStream.close();

        } catch (MalformedURLException e) {

            logger.debug(e.getMessage());

        } catch (IOException e) {

            logger.debug(e.getMessage());
        }

        return httpURLConnection;

    }

    public static String getResponseISO88591(HttpURLConnection httpURLConnection) {

        String xmlResponse = StringUtils.EMPTY;

        try {

            int status = httpURLConnection.getResponseCode();

            if (HttpURLConnection.HTTP_OK == status) {
                BufferedReader reader =
                                new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),
                                                CharEncoding.ISO_8859_1));
                String decodedString = null;
                StringBuilder builder = new StringBuilder();
                while ((decodedString = reader.readLine()) != null) {

                    builder.append(decodedString);
                    builder.append("\n");

                }
                xmlResponse = builder.toString();
                reader.close();
            }
        } catch (IOException e) {

            logger.debug(e.getMessage());
        }

        return xmlResponse;
    }
}
